package com.hockeo.shared.rpc.classic;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.hockeo.shared.VHUtil;
import com.hockeo.shared.rpc.classic.error.RpcNotAnAdminException;
import com.hockeo.shared.rpc.classic.error.RpcNotAuthenticatedException;

/**
 * Standard callback for asynchronous procedure calls.
 *
 * @param <T> the class of the object that is returned by the an RPC call
 * @version $Id$
 * @author jjanke
 */
public abstract class RpcCallback<T> implements AsyncCallback<T>
{
  @Override
  public void onFailure( Throwable t )
  {
    if ( t instanceof RpcNotAuthenticatedException )
    {
      // redirect to the login page
      //
      Window.open( ( (RpcNotAuthenticatedException) t ).getLoginUrl(), "_self", "" );
      return;
    }

    if ( t instanceof RpcNotAnAdminException )
    {
      Window.alert( VHUtil.constants().notAnAdminFailure() );
      return;
    }

    // TODO: better error handling
    Window.alert( t.toString() );
  }

  @Override
  public void onSuccess( T result )
  {
    processResult( result );
  }

  /**
   * This method gets called when an RPC call successfully returned some kind of answer.
   * Override this method to handle the result according to your specific needs.
   *
   * @param result the result to be processed
   */
  public abstract void processResult( T result );
}
